package com.quectel.core.module.patrol.service.impl;


import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.patrol.dao.VideoPatrolTaskDao;
import com.quectel.core.module.patrol.dto.VideoPatrolTaskDto;
import com.quectel.core.module.patrol.entity.VideoPatrolTaskEntity;
import com.quectel.core.module.patrol.service.VideoPatrolTaskService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2020-07-07 15:47:56
 */
@DubboService
public class VideoPatrolTaskServiceImpl implements VideoPatrolTaskService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VideoPatrolTaskServiceImpl.class);

    @Autowired
    private VideoPatrolTaskDao videoPatrolTaskDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private DeviceService deviceService;


    @Override
    public Map<String, Long> taskExecInfo(Map<String, Object> params) {
        return videoPatrolTaskDao.taskExecInfo(params);
    }

    @Override
    public List<VideoPatrolTaskDto> queryList(Map<String, Object> params) {
        List<VideoPatrolTaskEntity> list = videoPatrolTaskDao.queryList(params);
        List<VideoPatrolTaskDto> result = CopyUtils.copyList(list, VideoPatrolTaskDto.class);
        for (VideoPatrolTaskDto videoPatrolPlanDto : result) {
            VillageDto villageDto = villageService.selectById(videoPatrolPlanDto.getVillageId());
            if (villageDto != null) {
                videoPatrolPlanDto.setVillageDto(villageDto);
            }

        }
        return result;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return videoPatrolTaskDao.queryTotal(params);
    }

    @Override
    public Long save(VideoPatrolTaskDto dto) {
        VideoPatrolTaskEntity entity = CopyUtils.copyObj(dto, VideoPatrolTaskEntity.class);
        entity.setId(Snowflake.nextId());
        videoPatrolTaskDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(VideoPatrolTaskDto dto) {
        VideoPatrolTaskEntity entity = CopyUtils.copyObj(dto, VideoPatrolTaskEntity.class);
        videoPatrolTaskDao.updateById(entity);
    }

    @Override
    public VideoPatrolTaskDto selectById(Long id) {
        VideoPatrolTaskEntity entity = videoPatrolTaskDao.selectById(id);
        VideoPatrolTaskDto videoPatrolTaskDto = CopyUtils.copyObj(entity, VideoPatrolTaskDto.class);
        if (videoPatrolTaskDto != null) {
            if (StringUtils.isNotBlank(videoPatrolTaskDto.getRouteArr())) {
                List<Long> deviceIds = JacksonUtils.parseArray(videoPatrolTaskDto.getRouteArr(), Long.class);
                List<DeviceDto> deviceDtoList = new ArrayList<>();
                for (Long deviceId : deviceIds) {
                    DeviceDto deviceDto = deviceService.selectByIdWithoutPadding(deviceId);
                    if (deviceDto != null) {
                        deviceDtoList.add(deviceDto);
                    }
                }
                if (CollectionUtils.isNotEmpty(deviceDtoList)) {
                    videoPatrolTaskDto.setDeviceDtoList(deviceDtoList);
                }
            }
        }
        return videoPatrolTaskDto;
    }

    @Override
    public void deleteById(Long id) {
        videoPatrolTaskDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
